package cn.wenhe9.yygh.order.controller.api;

import cn.wenhe9.yygh.common.result.ResultResponse;
import cn.wenhe9.yygh.common.utils.AuthContextHolder;
import cn.wenhe9.yygh.enums.OrderStatusEnum;
import cn.wenhe9.yygh.model.order.OrderCountQueryVo;
import cn.wenhe9.yygh.model.order.OrderInfo;
import cn.wenhe9.yygh.model.vo.order.OrderQueryVo;
import cn.wenhe9.yygh.order.service.OrderService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author DuJinliang
 * 2022/4/6
 */
@Api(tags = "订单接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {
    @Resource
    private OrderService orderService;

    /**
     * 生成订单
     * @param scheduleId String
     * @param patientId String
     * @return ResultResponse
     */
    @ApiOperation("生成订单")
    @PostMapping("/auth/submitOrder/{scheduleId}/{patientId}")
    public ResultResponse saveOrders(
            @PathVariable(name = "scheduleId") String scheduleId,
            @PathVariable(name = "patientId") Long patientId
    ){
        Long orderId = orderService.saveOrder(scheduleId, patientId);
        return ResultResponse.ok(orderId);
    }

    /**
     * 根据orderId获取订单
     * @param orderId String
     * @return ResultResponse
     */
    @ApiOperation("根据orderId获取订单")
    @GetMapping("/auth/order/{orderId}")
    public ResultResponse getOrdersById(@PathVariable(name = "orderId") Long orderId){
        OrderInfo orderInfo = orderService.getOrderById(orderId);
        if (orderInfo == null){
            return ResultResponse.fail();
        }
        return ResultResponse.ok(orderInfo);
    }

    /**
     * 分页查询挂号订单
     * @param page Long
     * @param limit Long
     * @param orderQueryVo OrderQueryVo
     * @return ResultResponse
     */
    @ApiOperation("分页查询挂号订单")
    @GetMapping("/auth/{page}/{limit}")
    public ResultResponse getOrderList(
            @PathVariable(name = "page") Long page,
            @PathVariable(name = "limit") Long limit,
            OrderQueryVo orderQueryVo,
            HttpServletRequest request
    ){
        //设置当前用户id
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        IPage<OrderInfo> pageModel = orderService.getOrderListByPage(pageParam, orderQueryVo);
        return ResultResponse.ok(pageModel);
    }

    /**
     * 获取订单状态
     * @return ResultResponse
     */
    @ApiOperation("获取订单状态")
    @GetMapping("/auth/statusList")
    public ResultResponse getStatusList(){
        return ResultResponse.ok(OrderStatusEnum.getStatusList());
    }

    /**
     * 取消订单
     * @param orderId Long
     * @return ResultResponse
     */
    @ApiOperation("取消订单")
    @GetMapping("/auth/cancelOrder/{orderId}")
    public ResultResponse cancelOrder(@PathVariable(name = "orderId") Long orderId){
        Boolean isCancel =  orderService.cancelOrder(orderId);
        return ResultResponse.ok(isCancel);
    }

    /**
     * 获取订单统计数据
     * @param orderCountQueryVo OrderCountQueryVo
     * @return Map<String, Object>
     */
    @ApiOperation("获取订单统计数据")
    @GetMapping("/inner/countMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo){
        return orderService.getCountMap(orderCountQueryVo);
    }
}
